package cn.xxm.strategy.pay.i18n.response;

import cn.xxm.strategy.pay.i18n.utils.FI18nUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;

import java.io.Serializable;

/**
 * F Standard Response Model
 */
public class FSResponseModel<T> implements ResponseInfo, Serializable {

	private static final long serialVersionUID = 241501034771846742L;

	// 状态码
	private String code;
	// 业务提示语
	private String msg;
	// 数据对象
	private T data;
	/**
	 * 无参构造，默认交易成功的响应信息
	 */
	public FSResponseModel() {
		setCodeAndMsg(CommonResponseInfo.SUCCESS);
	}

	/**
	 * @param {@link FSResponseModel}
	 */
	public FSResponseModel(FSResponseModel<?> responseInfo) {
		setCodeAndMsg(responseInfo);
	}

	/**
	 * @param {@link FSResponseModel},T data
	 */
	public FSResponseModel(FSResponseModel<?> responseModel, T data) {
		setModel(responseModel, data);
	}

	/**
	 * @param <E extends Enum<?> E responseInfo,T data
	 */
	public <E extends Enum<?> & ResponseInfo> FSResponseModel(E responseInfo) {
		setCodeAndMsg(responseInfo);
	}

	/**
	 * @param <E extends Enum<?> E responseInfo,T data
	 */
	public <E extends Enum<?> & ResponseInfo> FSResponseModel(E responseInfo, T data) {
		setModel(responseInfo, data);
	}

	/**
	 * 通过实现ResponseInfo接口的FSResponseModel设置响应码和响应信息
	 */
	@JsonIgnore
	public void setCodeAndMsg(FSResponseModel<?> responseModel) {
		this.code = responseModel.getCode();
		this.msg = responseModel.getMsg();
	}

	/**
	 * 通过实现ResponseInfo接口的枚举设置响应码和响应信息
	 */
	@JsonIgnore
	public <E extends Enum<?> & ResponseInfo> void setCodeAndMsg(E responseInfo) {
		this.code = responseInfo.getCode();
		this.msg = getI18nMsg(responseInfo);
	}

	/**
	 * 通过实现ResponseInfo接口的FSResponseModel设置响应码和响应信息，以及响应数据data
	 */
	@JsonIgnore
	public void setModel(FSResponseModel<?> model, T data) {
		setCodeAndMsg(model);
		this.data = data;
	}

	/**
	 * 通过实现ResponseInfo接口的枚举设置响应码和响应信息，以及响应数据data
	 */
	public <E extends Enum<?> & ResponseInfo> void setModel(E responseInfo, T data) {
		setCodeAndMsg(responseInfo);
		this.data = data;
	}

	public FSResponseModel(String code, String msg, T data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
	}

	public FSResponseModel(String code, String msg) {
		this.code = code;
		this.msg = msg;
		this.data = null;
	}

	@Override
	public String getCode() {
		return code;
	}

	@Override
	public String getMsg() {
		return msg;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	/**
	 * 获取国际化响应信息
	 */
	public static String getI18nMsg(ResponseInfo responseInfo) {
		String oriMsg = responseInfo.getMsg();
		String i18nKey = responseInfo.getClass().getSimpleName().replace("ResponseInfo", "").toLowerCase() + "."
				+ oriMsg;
		String msg = FI18nUtil.getI18nValue(i18nKey);
		return msg == null ? oriMsg : msg;
	}

}
